/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c) 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *  
 *  
 * @original author: jin Chen 
 * @author: jin Chen 
 * @date: Mar 14, 2007$
 * @version: 1.0
 */
package edu.psu.geovista.app.visualclassifier.classify.v2.control;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import edu.psu.geovista.app.visualclassifier.classify.v2.ESVisualClassifier;
import edu.psu.geovista.classification.setting.ClassifySetting;
import edu.psu.geovista.classification.setting.ClassifySetting1D;
import epoviz.trex.common.xml.xstream.AbstractXSerializable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

@XStreamAlias("Visual Classifier Persistence")
public class PersistenceManager extends AbstractXSerializable  {
    transient ESVisualClassifier vc;
    transient String root;// 

    private Map<Object,ClassifySetting> classifySettings;

    public PersistenceManager() {
    }

    public PersistenceManager(ESVisualClassifier vc) {
        this.vc = vc;
    }

    /*******************************************************************************************************
     *                 services methods
     *******************************************************************************************************/
    public void save() throws IOException {
        setClassifySettings(vc.getClassifySettings());
        super.save();
    }
   public void load() throws FileNotFoundException {
       super.load();

    }
    /*******************************************************************************************************
     *                interface methods
     *******************************************************************************************************/
    public Object load(File file) throws FileNotFoundException {
        PersistenceManager pm = (PersistenceManager) loadInstance(file);
        Map<Object, ClassifySetting> settings = pm.getClassifySettings();
        if(settings !=null&& settings.size() >0){
           // System.out.println(this.getClass().getName() +" successfully load "+ settings.size()+"  classify settings.");
            vc.setClassifySettings(settings);
           // this.setClassifySettings(settings);
            return settings;
       }
       else{
           //System.err.println(this.getClass().getName() +" fail to load  classify settings.");
            return null;
       }
    }

    public Class[] getAliasClasses() {
        return new Class[]{ClassifySetting1D.class};  //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getPersistentFileName() {
        return this.getRoot() +File.separator+ "visualclassifier.xml";
    }
    /*******************************************************************************************************
     *                properties
     *******************************************************************************************************/
     public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }
    /**************************************************
     *      non-transient properties ( need to save)
     ***************************************************/
    public Map<Object, ClassifySetting> getClassifySettings() {
        return classifySettings;
    }

    public void setClassifySettings(Map<Object, ClassifySetting> classifySettings) {
        this.classifySettings = classifySettings;
    }
}
